import threading
from PIL import Image

lock = threading.Lock()
global_store = {}


def image_size_in_mb(image):
    # 获取图像的宽度和高度
    width, height = image.size

    # 获取图像的颜色模式和通道数
    mode = image.mode
    if mode == 'RGB':
        channels = 3
    elif mode == 'RGBA':
        channels = 4
    elif mode == 'L':
        channels = 1
    else:
        raise ValueError(f"Unsupported image mode: {mode}")

    # # 每个通道的位数
    # bits = {mode: image.getextrema()[2] for mode in
    #         ['1', 'L', 'P', 'RGB', 'RGBA', 'CMYK', 'YCbCr', 'LAB', 'HSV', 'I', 'F']}

    # 计算每个像素占用的字节数
    bytes_per_pixel = channels

    # 计算整个图像的总字节数
    total_bytes = width * height * bytes_per_pixel

    # 转换为兆字节
    memory_usage_mb = total_bytes / (1024 ** 2)

    # print(f"Estimated memory usage: {memory_usage_mb:.2f} MB")
    return memory_usage_mb

